#!/usr/bin/env python
# -*- coding: utf-8 -*-

try:
	from Tkinter import *
except ImportError:
	from tkinter import *

import sys
from itertools import cycle



class GUI :

	def __init__(self, local_dict):
		
		self.local_dict      = local_dict
		self.ring            = cycle(local_dict.keys())
		self.key             = next(self.ring)
		self.previous        = self.key
		self.master          = Tk()
		self.previous_button = Button(self.master, text = "Previous", command = lambda: self.prev())
		self.next_button     = Button(self.master, text = "Next", command = lambda: self.next())
		self.exit_button     = Button(self.master, text = "Exit", command = sys.exit)			
		self.next()
		self.master.mainloop()
	
	def prev(self):
	
		self.next(self.previous)
	
	def next(self, previous = None):
		
		self.previous = self.key
		self.key      = previous if previous is not None else next(self.ring)
		one_dict      = self.local_dict.get(self.key)
		self.drop_all_labels()
		self.populate(one_dict)
		
	def drop_all_labels(self):
		
		for o in filter(lambda chd: chd.winfo_class() == "Label", self.master.grid_slaves()): 
			o.destroy()

	def populate(self, one_dict):
		
		i = 0
		for k, v in one_dict.items():
			Label(self.master, text = self.strip(k)).grid(row = i, column = 0)
			Label(self.master, text = self.strip(v)).grid(row = i, column = 1)
			i += 1
		self.previous_button.grid(row = i, column = 0)
		self.next_button.grid(row = i, column = 1)
		self.exit_button.grid(row = i, column = 2)

	def strip(self, tuple):
		
		for chr in [",", "(", ")", "'"]:
			tuple = str(tuple).replace(chr, "")
		return tuple
	
